package cur.nature.component.redis.controller;

import cur.nature.component.redis.common.DelayQueueUtil;
import cur.nature.framework.core.log.LOG;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 缓存 Controller
 * 作者：Auto Generator By wujian
 * 生成日期：2023-08-05 17:57:55
 */
@Tag(name = "延迟队列 接口")
@RestController
@RequestMapping("component/redis/v1/api/delay")
@ConditionalOnProperty( value = {"cur.component.redis.controller.enable"})
public class Redis_DelayController {

    @Operation(summary = "设置 延迟队列")
    @GetMapping("/set")
    public void set() throws Exception {
        LOG.info("set Redisson");
        for (int i = 1; i < 10; i++) {
            DelayQueueUtil.getDelayQueue().offer("message " + i, 10*i, TimeUnit.SECONDS);
        }
    }

    @Operation(summary = "获取 延迟队列")
    @GetMapping("/get")
    public Map<String, String> get() throws Exception {
        Long curtime = System.currentTimeMillis();
        new Thread(()->{
            while (true) {
                try {
                    String take = DelayQueueUtil.getBlockQueue().take();
                    long diff = (System.currentTimeMillis() - curtime)/1000L;
                    LOG.info("时间:" +diff+ " 秒, message : " + take );
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

        Map resMap = new HashMap<String, String>();
        resMap.put("message", "");
        return resMap;
    }

}

